package com.flycms.web.document;

import com.flycms.core.entity.PageBean;
import com.flycms.core.entity.PageVo;
import com.flycms.core.entity.RestDataVo;
import com.flycms.module.document.enums.DocTypeEnum;
import com.flycms.module.document.model.Document;
import com.flycms.module.document.model.DocumentDetail;
import com.flycms.module.document.param.DocQueryParam;
import com.flycms.module.document.param.DocumentParam;
import com.flycms.module.document.service.DocumentService;
import com.flycms.utils.ConvertUtils;
import com.flycms.web.document.vo.DocSimpleVO;
import com.flycms.web.document.vo.DocumentVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    /**
     * 保存文档
     * @param param 文档数据
     * @return 保存结果
     */
    @PostMapping("/system/doc/save")
    public RestDataVo save(@RequestBody DocumentParam param){
        try{
            documentService.save(param);
        } catch (Exception e){
            return RestDataVo.ERROR("保存失败");
        }
        return RestDataVo.OK("保存成功");
    }

    /**
     * 获取文档列表
     * @return 文档列表集合
     */
    @GetMapping("/doc/list")
    public RestDataVo getDocList(){
        List<DocumentVO> documentVOS = Lists.newArrayList();
        for (DocTypeEnum anEnum : DocTypeEnum.values()) {
            List<Document> documents = documentService.getDocList(anEnum.getCode());
            DocumentVO documentVO = DocumentVO.builder()
                    .docType(anEnum.getCode())
                    .docTypeName(anEnum.getDesc())
                    .build();
            if(CollectionUtils.isNotEmpty(documents)){
                documentVO.setDocSimpleVOS(ConvertUtils.sourceToTarget(documents, DocSimpleVO.class));
            }
            documentVOS.add(documentVO);
        }
        return RestDataVo.OK(documentVOS);
    }

    /**
     * 获取文档列表
     * @return 文档列表集合
     */
    @GetMapping("/system/doc/list")
    public RestDataVo getDocList(DocQueryParam param){
        PageVo<Document> pageData = documentService.getPage(param);
        if(CollectionUtils.isEmpty(pageData.getList())){
            return RestDataVo.PAGE(pageData.getList(),pageData.getPageNum(),param.getPageSize(),0);
        }
        return RestDataVo.PAGE(pageData.getList(),pageData.getPageNum(),param.getPageSize(),pageData.getCount());
    }

    /**
     * 根据名称模糊匹配文档
     * @param name 文档名称
     * @return 匹配集合
     */
    @GetMapping("/doc/list/like")
    public RestDataVo getDocByName(String name){
        List<DocumentVO> documentVOS = Lists.newArrayList();
        List<Document> documents = documentService.getDocByName(name);
        if(CollectionUtils.isEmpty(documents)){
            return RestDataVo.OK(documentVOS);
        }
        Map<Integer, List<Document>> documentMap = documents.stream()
                .collect(Collectors.groupingBy(Document::getDocType));
        for (DocTypeEnum anEnum : DocTypeEnum.values()) {
            DocumentVO documentVO = DocumentVO.builder()
                    .docType(anEnum.getCode())
                    .docTypeName(anEnum.getDesc())
                    .build();
            List<Document> documentList = documentMap.get(anEnum.getCode());
            if(CollectionUtils.isNotEmpty(documentList)){
                documentVO.setDocSimpleVOS(ConvertUtils.sourceToTarget(documents, DocSimpleVO.class));
            }
            documentVOS.add(documentVO);
        }
        return RestDataVo.OK(documentVOS);
    }

    /**
     * 查询文档详情
     * @param id 文档ID
     * @return 文档详情
     */
    @GetMapping("/doc/{id}")
    public RestDataVo getDocDetail(@PathVariable("id") Long id){
        Document document = documentService.getDocById(id);
        if(Objects.isNull(document)){
            return RestDataVo.OK(null);
        }
        DocumentDetail documentDetail = documentService.getDocDetailById(document.getId());
        return RestDataVo.OK(documentDetail);
    }

    /**
     * 查询文档详情
     * @param id 文档ID
     * @return 文档详情
     */
    @GetMapping("/system/doc/{id}")
    public RestDataVo getSysDocDetail(@PathVariable("id") Long id){
        Document document = documentService.getDocById(id);
        if(Objects.isNull(document)){
            return RestDataVo.OK(null);
        }
        DocumentDetail documentDetail = documentService.getDocDetailById(document.getId());
        document.setDetail(documentDetail);
        return RestDataVo.OK(document);
    }

    /**
     * 查询文档详情
     * @param id 文档ID
     * @return 文档详情
     */
    @DeleteMapping("/system/doc/del/{id}")
    public RestDataVo delDoc(@PathVariable("id") Long id){
        try{
            documentService.del(id);
        } catch (Exception e){
            return RestDataVo.ERROR("删除失败");
        }
        return RestDataVo.OK("删除成功");
    }

    /**
     * 更新文档
     */
    @PutMapping("/system/doc/update")
    public RestDataVo updateDoc(@RequestBody Document document){
        try{
            documentService.update(document);
        } catch (Exception e){
            log.error("更新失败",e);
            return RestDataVo.ERROR("更新失败");
        }
        return RestDataVo.OK("更新成功");
    }

}
